package com.smart.translator.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

public class DraggableCircleView extends View {
    private Paint paint;
    private Paint deletePaint;
    private float radius = 50f;
    private int number;
    private float lastX, lastY;
    private static final float MIN_RADIUS = 30f;
    private static final float MAX_RADIUS = 100f;
    private boolean isScaling = false;
    private float initialDistance;
    private float initialRadius;
    private PointF center = new PointF();
    private RectF deleteButtonRect;
    private boolean isInDeleteMode = false;
    private OnDeleteListener onDeleteListener;

    public interface OnDeleteListener {
        void onDelete(DraggableCircleView view);
    }

    public void setOnDeleteListener(OnDeleteListener listener) {
        this.onDeleteListener = listener;
    }

    public DraggableCircleView(Context context) {
        super(context);
        init();
    }

    public DraggableCircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.parseColor("#80000000")); // 半透明黑色
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(30f);

        deletePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        deletePaint.setColor(Color.RED);
        deletePaint.setTextAlign(Paint.Align.CENTER);
        deletePaint.setTextSize(24f);
        
        deleteButtonRect = new RectF();
    }

    public void setNumber(int number) {
        this.number = number;
        invalidate();
    }

    public int getNumber() {
        return number;
    }

    public float getRadius() {
        return radius;
    }

    public void setRadius(float radius) {
        this.radius = Math.max(MIN_RADIUS, Math.min(MAX_RADIUS, radius));
        updateDeleteButtonRect();
        
        // 更新View的大小
        ViewGroup.LayoutParams params = getLayoutParams();
        if (params != null) {
            int newSize = (int) (this.radius * 2);
            params.width = newSize;
            params.height = newSize;
            setLayoutParams(params);
        }
        
        invalidate();
    }

    public void showEditMode(boolean show) {
        isInDeleteMode = show;
        invalidate();
    }

    public boolean isInDeleteMode() {
        return isInDeleteMode;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        updateDeleteButtonRect();
    }

    private void updateDeleteButtonRect() {
        float deleteButtonSize = radius * 0.4f;
        deleteButtonRect.set(
            getWidth() - deleteButtonSize * 1.5f,
            deleteButtonSize * 0.5f,
            getWidth() - deleteButtonSize * 0.5f,
            deleteButtonSize * 1.5f
        );
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        
        // 绘制圆形
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(isInDeleteMode ? Color.parseColor("#80FF0000") : Color.parseColor("#80000000"));
        canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, radius, paint);

        // 绘制数字
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.FILL);
        float textHeight = paint.descent() - paint.ascent();
        float textOffset = textHeight / 2 - paint.descent();
        canvas.drawText(String.valueOf(number), 
                       getWidth() / 2f, 
                       getHeight() / 2f + textOffset, 
                       paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 确保触摸事件能被正确处理
        android.util.Log.d("DraggableCircleView", "View触摸事件: " + event.getActionMasked() + ", 指针数量: " + event.getPointerCount());
        return super.onTouchEvent(event);
    }

    public void savePosition() {
        center.set(getX() + getWidth() / 2f, getY() + getHeight() / 2f);
    }

    public PointF getCenter() {
        return center;
    }
} 